package com.teen9g.infoplatformserver.controller;

import com.github.pagehelper.PageInfo;
import com.teen9g.dto.CommentDTO;
import com.teen9g.infoplatformserver.service.CommentService;
import com.teen9g.result.Result;
import com.teen9g.vo.CommentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 发布评论
     *
     * @param commentDTO 评论信息
     * @return 发布结果
     */
    @PostMapping("/publish")
    public Result<String> publishComment(@RequestBody CommentDTO commentDTO) {
        log.info("发布评论：{}", commentDTO);
        String result = commentService.publishComment(commentDTO);
        return Result.success(result);
    }

    /**
     * 发布评论回复
     *
     * @param commentDTO 评论信息
     * @return 发布结果
     */
    @PostMapping("/publish/reply")
    public Result<String> publishCommentReply(@RequestBody CommentDTO commentDTO) {
        log.info("发布评论回复：{}", commentDTO);
        String result = commentService.publishReply(commentDTO);
        return Result.success(result);
    }

    /**
     * 删除评论
     *
     * @param commentId 评论ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{commentId}")
    public Result<String> deleteComment(@PathVariable Integer commentId) {
        log.info("删除编号为{}的评论", commentId);
        String result = commentService.deleteComment(commentId);
        return Result.success(result);
    }

    /**
     * 获取评论树
     *
     * @param relatedId       相关ID
     * @param relatedCategory 相关类别
     * @param pageNum         页码
     * @param pageSize        每页大小
     * @param orderBy         排序方式
     * @return 评论树
     */
    @GetMapping("/page")
    public Result<PageInfo<CommentVO>> getCommentTree
            (@RequestParam Integer relatedId,
             @RequestParam String relatedCategory,
             @RequestParam(defaultValue = "1") Integer pageNum,
             @RequestParam(defaultValue = "10") Integer pageSize,
             @RequestParam(defaultValue = "like") String orderBy) {
        log.info("分页获取编号为{}的评论树，类别：{}，页码：{}，每页大小：{}，排序：{}", relatedId, relatedCategory, pageNum, pageSize, orderBy);
        PageInfo<CommentVO> pageInfo = commentService.getCommentTree(relatedId, relatedCategory, pageNum, pageSize, orderBy);
        return Result.success(pageInfo);
    }

    /**
     * 点赞/取消点赞
     *
     * @param commentId 评论ID
     * @return 点赞结果
     */
    @PostMapping("/likeChange/{commentId}")
    public Result<String> likeChange(@PathVariable Integer commentId) {
        log.info("点赞/取消点赞，评论编号：{}", commentId);
        String result = commentService.likeChange(commentId);
        return Result.success(result);
    }

    /**
     * 检查是否点赞
     * 用于前端的点赞图标的变化
     *
     * @param commentId 评论ID
     * @param userId    用户ID
     * @return 点赞结果
     */
    @GetMapping("/checkLike")
    public Result<Integer> checkLike(@RequestParam Integer commentId,
                                     @RequestParam Integer userId) {
        log.info("检查是否点赞，评论编号：{}，用户ID：{}", commentId, userId);
        Integer result = commentService.checkLike(commentId, userId);
        return Result.success(result);
    }
}
